package org.meetunexpectedly.service.video.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.meetunexpectedly.entity.video.VideoThumb;
import org.meetunexpectedly.mapper.video.VideoThumbMapper;
import org.meetunexpectedly.service.video.VideoThumbService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 视频点赞服务实现类
 * @Author: pupil
 * @Date: 2024/08/22 下午 7:11
 */
@Service
public class VideoThumbServiceImpl extends ServiceImpl<VideoThumbMapper, VideoThumb> implements VideoThumbService {

    @Override
    public Boolean thumbUp(VideoThumb videoThumb) {
        try {
            // 添加点赞
            this.save(videoThumb);
        } catch (Exception e) {
            // 存在则取消点赞
            this.remove(new LambdaQueryWrapper<VideoThumb>().eq(VideoThumb::getVideoId, videoThumb.getVideoId()).eq(VideoThumb::getUserId, videoThumb.getUserId()));
            return false;
        }
        return true;
    }

    @Override
    public List<Long> getThumbUserId(Long videoId) {
        return this.list(new LambdaQueryWrapper<VideoThumb>().eq(VideoThumb::getVideoId, videoId)).stream().map(VideoThumb::getUserId).collect(Collectors.toList());
    }

    @Override
    public Boolean thumbUpState(Long videoId, Long userId) {
        if (userId == null) return false;
        // 点赞数为1则为已点赞  点赞数为0则未点赞
        return this.count(new LambdaQueryWrapper<VideoThumb>().eq(VideoThumb::getVideoId, videoId).eq(VideoThumb::getUserId, userId)) == 1;
    }
}
